﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace LibraryManagementSystem
{
    public partial class 首页 : Form
    {
        //声明自适应类实例
        AutoSizeFormClass asc = new AutoSizeFormClass();
        VerificationCodeClass vcc = new VerificationCodeClass();

        public 首页()
        {
            InitializeComponent();
            this.Resize += new System.EventHandler(首页_Resize);
        }

        #region 调整控件大小
        private void 首页_Resize(object sender,EventArgs e)
        {
            asc.controlAutoSize(this);
        }

        private void 首页_Load(object sender, EventArgs e)
        {
            asc.controllInitializeSize(this);
            Bitmap bmp = vcc.setCode();
            codePicBox.Image = bmp;
        }
        #endregion

        //跳转到注册页面
        private void registerLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            注册页面 f = new 注册页面();
            f.Show();
        }
        //登录后跳转
        private void loginBtn_Click(object sender, EventArgs e)
        {
            //获取登录信息
            string mail = mailTxb.Text;
            string pwd = pwdTxb.Text;
            string code = codeTxb.Text;
            

            if (mail.Trim() == "" || pwd.Trim() == "")
            {
                MessageBox.Show("用户名和密码不能为空", "Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (code.Trim() == "")
            {
                MessageBox.Show("请输入验证码", "Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (code == vcc.txt)
                {
                    //连接数据库
                    string conPath = "Data Source = ../../test.db";
                    using (SQLiteConnection con = new SQLiteConnection(conPath))
                    {
                        con.Open();
                        string sql;
                        SQLiteCommand cmd;
                        try
                        {
                            if (adminRBtn.Checked == true)
                            {
                                sql = "select adminMail from admin where adminMail='" + mail + "' and adminPwd='" + pwd + "'";
                                cmd = new SQLiteCommand(sql, con);

                                if (cmd.ExecuteScalar() != null)
                                {
                                    管理员页面 f = new 管理员页面();
                                    f.Id = mail;//将账号传给页面
                                    f.ShowDialog();
                                }
                                else
                                {
                                    MessageBox.Show("账号或密码错误", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    pwdTxb.Clear();
                                    codeTxb.Clear();
                                    Bitmap bmp = vcc.setCode();
                                    codePicBox.Image = bmp;
                                }

                            }
                            else
                            {
                                sql = "select userMail from user where userMail='" + mail + "' and adminPwd='" + pwd + "'";
                                cmd = new SQLiteCommand(sql, con);

                                if (cmd.ExecuteScalar() != null)
                                {
                                    //用户页面 f = new 用户页面();

                                    f.ShowDialog();
                                }
                                else
                                {
                                    MessageBox.Show("账号或密码错误", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    pwdTxb.Clear();
                                    codeTxb.Clear();
                                    Bitmap bmp = vcc.setCode();
                                    codePicBox.Image = bmp;
                                }

                            }
                        }
                        catch { }
                       
                    }
                        
                }
                else
                {
                    MessageBox.Show("验证码错误", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Bitmap bmp = vcc.setCode();
                    codePicBox.Image = bmp;
                    codeTxb.Clear();
                }
            }

        }

        private void pwdChk_CheckedChanged(object sender, EventArgs e)
        {
            if (pwdChk.Checked)
            {
                pwdTxb.PasswordChar = '\0';//显示文本
            }
            else
            {
                pwdTxb.PasswordChar = '*';
            }
        }

        private void codePicBox_Click(object sender, EventArgs e)
        {
            Bitmap bmp = vcc.setCode();
            codePicBox.Image = bmp;
        }
    }
}
